/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.data.Airline;
import easybooking.data.Airport;
import easybooking.data.Booking;
import easybooking.data.Customer;
import easybooking.data.Flight;
import easybooking.data.Plane;
import easybooking.data.Seat;
import easybooking.data.Type;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.OptionPaneUI;
import javax.swing.tree.TreePath;

/**
 *
 * @author tim
 */
public class EBInterface implements ActionListener, TreeSelectionListener {

    public static void main(String args[]) {
        UIManager.put("OptionPane.noButtonText", "No");
        UIManager.put("OptionPane.yesButtonText", "Yes");
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ebi = new EBInterface();
                ebg = new EBInterfaceGui();
                ebg.createAndShowGUI(ebi);
                ;
            }
        });
    }
    private static EBInterface ebi;
    private static EBInterfaceGui ebg;
    public ImageIcon StartPic;
    private JFrame clientMainFrame;
    private EBInterfaceClient ebic = new EBInterfaceClient();
    private EBInterfaceConfigDialog ebicd = new EBInterfaceConfigDialog(this);
    private CustomerForm cf = new CustomerForm(this);
    private CustomerDialog cd = new CustomerDialog(this);
    private TypeForm tf = new TypeForm(this);
    private TypeDialog td = new TypeDialog(this);
    private AirportForm airpf = new AirportForm(this);
    private AirportCreateDialog airpcd = new AirportCreateDialog(this);
    private AirportEditDialog airped = new AirportEditDialog(this);
    private PlaneForm pf = new PlaneForm(this);
    private PlaneDialogTypeSelect pdts = new PlaneDialogTypeSelect(this);
    private PlaneDialogAirlineSelect pdas = new PlaneDialogAirlineSelect(this);
    private PlaneDialog pd = new PlaneDialog(this);
    private AirlineForm airlf = new AirlineForm(this);
    private AirlineCreateDialog airlcd = new AirlineCreateDialog(this);
    private AirlineEditDialog airled = new AirlineEditDialog(this);
    private FlightForm ff = new FlightForm(this);
    private FlightDialog fd = new FlightDialog(this);
    private FlightDialogAirportSelect fdas = new FlightDialogAirportSelect(this);
    private FlightDialogPlaneSelect fdps = new FlightDialogPlaneSelect(this);
    private FlightDialogDateSelect fdds = new FlightDialogDateSelect(this);
    private BookingForm bf = new BookingForm(this);
    private BookingDialog bd = new BookingDialog(this);
    private BookingDialogCustomerSelect bdcs = new BookingDialogCustomerSelect(this);
    private BookingDialogFlightSelect bdfs = new BookingDialogFlightSelect(this);
    private BookingDialogSeatSelect bdss = new BookingDialogSeatSelect(this);

    public EBInterface() {
        //new EasyBookingInterfaceConfig(this);
        //this.createAndShowGUI();
    }

    public void setServer() {
        ebicd.createAndShowDialog();
    }

    public EBInterfaceClient getInterfaceClient() {
        return ebic;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("connectToServer")) {
            if (ebicd.getHostname().equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter a server address");
            } else if (!ebic.initServerConnection(ebicd.getHostname())) {
                JOptionPane.showMessageDialog(null, "Server connection failed");
            } else {
                ebicd.dispose();
            }
        }

        if (e.getActionCommand().equals("createCustomer")) {
            cd.createAndShowDialog();
        }

        if (e.getActionCommand().equals("customerDialogOk")) {
            boolean checkTextFields = true;
            if (cd.getFirstName().equals("") || cd.getLastName().equals("") || cd.getStreet().equals("") || cd.getCity().equals("") || cd.getZip().equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }

            JButton customerSource = (JButton) e.getSource();
            if (customerSource.getName().equals("createCustomer") && checkTextFields) {

                // validate input of the zip field
                try {
                    int x = Integer.parseInt(cd.getZip());
                } catch (NumberFormatException nFE) {
                    JOptionPane.showMessageDialog(null, "Please enter a correct Zip-Code");
                }
                // adds a new customer example data
                ebic.createCustomer(cd.getFirstName(), cd.getLastName(), cd.getStreet(), cd.getZip(), cd.getCity());
                // gets and sets the whole table data
                cf.setTableData(ebic.getAllCustomers());
                // cleans the view
                cf.removeAll();
                // renews the cf
                cf.createAndShowForm();
                // shows the cf again
                ebg.setRightPanel(cf);
                // closes the ccd
                cd.dispose();
            }
            if (customerSource.getName().equals("editCustomer") && checkTextFields) {
                // validate input of the zip field
                try {
                    int x = Integer.parseInt(cd.getZip());
                } catch (NumberFormatException nFE) {
                    JOptionPane.showMessageDialog(null, "Please enter a correct Zip-Code");
                }
                // update table before updating customer
                cf.setTableData(ebic.getAllCustomers());
                // adds a new customer example data
                ebic.updateCustomer(cd.getFirstName(), cd.getLastName(), cd.getStreet(), cd.getZip(), cd.getCity(), cd.getEditableCustomer());
                // gets and sets the whole table data
                cf.setTableData(ebic.getAllCustomers());
                // cleans the view
                cf.removeAll();
                // renews the cf
                cf.createAndShowForm();
                // shows the cf again
                ebg.setRightPanel(cf);
                // closes the ccd
                cd.dispose();
            }
        }

        if (e.getActionCommand().equals("editCustomer")) {
            if (cf.getSelectedCustomer() == null) {
                JOptionPane.showMessageDialog(null, "No customer selected");
            } else {
                Customer c = (Customer) cf.getSelectedCustomer();
                // pases the customer which should be edited
                cd.createAndShowDialog(c);
            }
        }
        if (e.getActionCommand().equals("deleteCustomer")) {
            if (cf.getSelectedCustomer() == null) {
                JOptionPane.showMessageDialog(null, "No customer selected");
            } else {
                try {
                    Customer c = (Customer) cf.getSelectedCustomer();
                    int i = JOptionPane.showConfirmDialog(clientMainFrame, "Delete Customer No: " + c.getCustomerId() + " " + c.getFirstname() + " " + c.getLastname() + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if (i == JOptionPane.YES_OPTION) {
                        cf.setTableData(ebic.getAllCustomers());
                        ebic.deleteCustomer(c);
                        cf.setTableData(ebic.getAllCustomers());
                        // cleans the view
                        cf.removeAll();
                        // renews the cf
                        cf.createAndShowForm();
                        // shows the cf again
                        ebg.setRightPanel(cf);
                    }
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        if (e.getActionCommand().equals("createType")) {
            td.createAndShowDialog();
        }

        if (e.getActionCommand().equals("typeDialogOk")) {
            boolean checkTextFields = true;
            if (td.getName().equals("") || td.getColumns().equals("") || td.getRows().equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }
            JButton typeSource = (JButton) e.getSource();
            if (typeSource.getName().equals("createType") && checkTextFields) {
                try {
                    int rows = Integer.parseInt(td.getRows());
                    int columns = Integer.parseInt(td.getColumns());
                } catch (NumberFormatException nFE) {
                    JOptionPane.showMessageDialog(null, "Please enter a correct number");
                }
                // update the table before creating type
                tf.setTableData(ebic.getAllTypes());
                // adds a new Type example data
                ebic.createType(td.getName(), td.getRows(), td.getColumns());
                // gets and sets the whole table data
                tf.setTableData(ebic.getAllTypes());
                // cleans the view
                tf.removeAll();
                // renews the tf
                tf.createAndShowForm();
                // shows the tf again
                ebg.setRightPanel(tf);
                // closes the tcd
                td.dispose();
            }
            if (typeSource.getName().equals("editType") && checkTextFields) {
                try {
                    int rows = Integer.parseInt(td.getRows());
                    int columns = Integer.parseInt(td.getColumns());
                } catch (NumberFormatException nFE) {
                    JOptionPane.showMessageDialog(null, "Please enter a correct Number");
                }
                // updating table before editing type
                tf.setTableData(ebic.getAllTypes());
                // adds a new customer example data
                ebic.updateType(td.getName(), Integer.valueOf(td.getRows()), Integer.valueOf(td.getColumns()), td.getEditableType());
                // gets and sets the whole table data
                tf.setTableData(ebic.getAllTypes());
                // cleans the view
                tf.removeAll();
                // renews the tf
                tf.createAndShowForm();
                // shows the ff again
                ebg.setRightPanel(tf);
                // closes the td
                td.dispose();
            }
        }

        if (e.getActionCommand().equals("editType")) {
            if (tf.getSelectedType() == null) {
                JOptionPane.showMessageDialog(null, "No type selected");
            } else {
                Type t = (Type) tf.getSelectedType();
                // pases the type which should be edited
                td.createAndShowDialog(t);
            }
        }

        if (e.getActionCommand().equals("deleteType")) {
            if (tf.getSelectedType() == null) {
                JOptionPane.showMessageDialog(null, "No Type selected");
            } else {
                try {
                    Type t = (Type) tf.getSelectedType();
                    int i = JOptionPane.showConfirmDialog(clientMainFrame, "Delete Type No: " + t.getTypeId() + " " + t.getName() + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if (i == JOptionPane.YES_OPTION) {
                        tf.setTableData(ebic.getAllTypes());
                        ebic.deleteType(t);
                        tf.setTableData(ebic.getAllTypes());
                        // cleans the view
                        tf.removeAll();
                        // renews the cf
                        tf.createAndShowForm();
                        // shows the cf again
                        ebg.setRightPanel(tf);
                    }
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        if (e.getActionCommand().equals("createAirport")) {
            airpcd.createAndShowDialog();
        }

        if (e.getActionCommand().equals("airportCreateDialogOk")) {
            boolean checkTextFields = true;
            if (airpcd.getName().equals("") || airpcd.getInitials().equals("") || airpcd.getCity().equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }
            if (checkTextFields) {
                airpf.setTableData(ebic.getAllAirports());
                // adds a new Type example data
                ebic.createAirport(airpcd.getName(), airpcd.getInitials(), airpcd.getCity());
                // gets and sets the whole table data
                airpf.setTableData(ebic.getAllAirports());
                // cleans the view
                airpf.removeAll();
                // renews the airpf
                airpf.createAndShowForm();
                // shows the airpf again
                ebg.setRightPanel(airpf);
                // closes the airpcd
                airpcd.dispose();
            }
        }

        if (e.getActionCommand().equals("editAirport")) {
            if (airpf.getSelectedAirport() == null) {
                JOptionPane.showMessageDialog(null, "No airport selected");
            } else {
                Airport airp = (Airport) airpf.getSelectedAirport();
                // pases the type which should be edited
                airped.createAndShowDialog(airp);
            }
        }

        if (e.getActionCommand().equals("airportEditDialogOk")) {
            boolean checkTextFields = true;
            if (airped.getName().equals("") || airped.getInitials().equals("") || airped.getCity().equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }
            if (checkTextFields) {
                airpf.setTableData(ebic.getAllAirports());
                // adds a new airport example data
                ebic.updateAirport(airped.getName(), airped.getInitials(), airped.getCity(), airped.getEditableAirport());
                // gets and sets the whole table data
                airpf.setTableData(ebic.getAllAirports());
                // cleans the view
                airpf.removeAll();
                // renews the airpf
                airpf.createAndShowForm();
                // shows the airpf again
                ebg.setRightPanel(airpf);
                // closes the airped
                airped.dispose();
            }
        }

        if (e.getActionCommand().equals("deleteAirport")) {
            if (airpf.getSelectedAirport() == null) {
                JOptionPane.showMessageDialog(null, "No airport selected");
            } else {
                try {
                    Airport airp = (Airport) airpf.getSelectedAirport();
                    int i = JOptionPane.showConfirmDialog(clientMainFrame, "Delete Airport: " + airp.getName() + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if (i == JOptionPane.YES_OPTION) {
                        airpf.setTableData(ebic.getAllAirports());
                        ebic.deleteAirport(airp);
                        airpf.setTableData(ebic.getAllAirports());
                        // cleans the view
                        airpf.removeAll();
                        // renews the cf
                        airpf.createAndShowForm();
                        // shows the cf again
                        ebg.setRightPanel(airpf);
                    }
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (e.getActionCommand().equals("createPlane")) {
            //pcd.setType(null);
            pd.createAndShowDialog();
        }
        if (e.getActionCommand().equals("editPlane")) {
            if (pf.getSelectedPlane() == null) {
                JOptionPane.showMessageDialog(null, "No plane selected");
            } else {
                try {
                    Plane p = (Plane) pf.getSelectedPlane();
                    pd.setAirline(ebic.findAirlineByKey(p.getAirlineId()));
                    pd.setType(ebic.findTypeByKey(p.getTypeId()));
                    pd.createAndShowDialog(p);
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (e.getActionCommand().equals("planeDialogOk")) {
            boolean checkTextFields = true;
            if (pd.getName().equals("") || pd.getAirline() == null || pd.getType() == null || pd.getType() == null || pd.getAirline() == null) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }
            JButton planeSource = (JButton) e.getSource();
            if (planeSource.getName().equals("createPlane") && checkTextFields) {
                try {
                    pf.setTableData(ebic.getAllPlanes());
                    // adds a new Type example data
                    ebic.createPlane(pd.getName(), pd.getType().getTypeId(), pd.getAirline().getAirlineId());
                    // gets and sets the whole table data
                    pf.setTableData(ebic.getAllPlanes());
                    // cleans the view
                    pf.removeAll();
                    // renews the airpf
                    pf.createAndShowForm();
                    // shows the airpf again
                    ebg.setRightPanel(pf);
                    // closes the airpcd
                    pd.dispose();
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (planeSource.getName().equals("editPlane") && checkTextFields) {
                try {
                    pf.setTableData(ebic.getAllPlanes());
                    // adds a new airport example data
                    ebic.updatePlane(pd.getName(), pd.getType().getTypeId(), pd.getAirline().getAirlineId(), pd.getEditablePlane());
                    // gets and sets the whole table data
                    pf.setTableData(ebic.getAllPlanes());
                    // cleans the view
                    pf.removeAll();
                    // renews the airpf
                    pf.createAndShowForm();
                    // shows the airpf again
                    ebg.setRightPanel(pf);
                    // closes the airped
                    pd.dispose();
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        if (e.getActionCommand().equals("PlaneDialogTypeSelect")) {
            pdts.setTableData(ebic.getAllTypes());
            pdts.createAndShowDialog();
        }

        if (e.getActionCommand().equals("PlaneSelectType")) {
            pd.setType((Type) pdts.getSelectedType());
            pdts.dispose();
        }

        if (e.getActionCommand().equals("PlaneDialogAirlineSelect")) {
            pdas.setTableData(ebic.getAllAirlines());
            pdas.createAndShowDialog();
        }

        if (e.getActionCommand().equals("PlaneSelectAirline")) {
            pd.setAirline((Airline) pdas.getSelectedAirline());
            pdas.dispose();
        }

        if (e.getActionCommand().equals("deletePlane")) {
            if (pf.getSelectedPlane() == null) {
                JOptionPane.showMessageDialog(null, "No plane selected");
            } else {
                try {
                    Plane p = (Plane) pf.getSelectedPlane();
                    int i = JOptionPane.showConfirmDialog(clientMainFrame, "Delete Plane: " + p.getName() + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if (i == JOptionPane.YES_OPTION) {
                        pf.setTableData(ebic.getAllPlanes());
                        ebic.deletePlane(p);
                        pf.setTableData(ebic.getAllPlanes());
                        // cleans the view
                        pf.removeAll();
                        // renews the cf
                        pf.createAndShowForm();
                        // shows the cf again
                        ebg.setRightPanel(pf);
                    }
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        if (e.getActionCommand().equals("createAirline")) {
            airlcd.createAndShowDialog();
        }

        if (e.getActionCommand().equals("airlineCreateDialogOk")) {
            boolean checkTextFields = true;
            if (airlcd.getName().equals("") || airlcd.getInitials().equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }
            if (checkTextFields) {
                airlf.setTableData(ebic.getAllAirlines());
                // adds a new Type example data
                ebic.createAirline(airlcd.getName(), airlcd.getInitials());
                // gets and sets the whole table data
                airlf.setTableData(ebic.getAllAirlines());
                // cleans the view
                airlf.removeAll();
                // renews the airpf
                airlf.createAndShowForm();
                // shows the airpf again
                ebg.setRightPanel(airlf);
                // closes the airpcd
                airlcd.dispose();
            }
        }

        if (e.getActionCommand().equals("editAirline")) {
            if (airlf.getSelectedAirline() == null) {
                JOptionPane.showMessageDialog(null, "No airline selected");
            } else {
                Airline airl = (Airline) airlf.getSelectedAirline();
                // pases the type which should be edited
                airled.createAndShowDialog(airl);
            }
        }

        if (e.getActionCommand().equals("airlineEditDialogOk")) {
            boolean checkTextFields = true;
            if (airled.getName().equals("") || airled.getInitials().equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }
            if (checkTextFields) {
                airlf.setTableData(ebic.getAllAirlines());
                // adds a new airport example data
                ebic.updateAirline(airled.getName(), airled.getInitials(), airled.getEditableAirline());
                // gets and sets the whole table data
                airlf.setTableData(ebic.getAllAirlines());
                // cleans the view
                airlf.removeAll();
                // renews the airpf
                airlf.createAndShowForm();
                // shows the airpf again
                ebg.setRightPanel(airlf);
                // closes the airped
                airled.dispose();
            }
        }

        if (e.getActionCommand().equals("deleteAirline")) {
            if (airlf.getSelectedAirline() == null) {
                JOptionPane.showMessageDialog(null, "No airline selected");
            } else {
                try {
                    airlf.setTableData(ebic.getAllAirlines());
                    Airline airl = (Airline) airlf.getSelectedAirline();
                    int i = JOptionPane.showConfirmDialog(clientMainFrame, "Delete Airline: " + airl.getName() + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if (i == JOptionPane.YES_OPTION) {
                        ebic.deleteAirline(airl);
                        airlf.setTableData(ebic.getAllAirlines());
                        // cleans the view
                        airlf.removeAll();
                        // renews the cf
                        airlf.createAndShowForm();
                        // shows the cf again
                        ebg.setRightPanel(airlf);
                    }
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (e.getActionCommand().equals("createFlight")) {
            fd.createAndShowDialog();
        }
        if (e.getActionCommand().equals("editFlight")) {
            try {
                if (ff.getSelectedFlight() == null) {
                    JOptionPane.showMessageDialog(null, "No flight selected");
                } else if (((Flight) ff.getSelectedFlight()).getDateTime().before(new Date())) {
                    JOptionPane.showMessageDialog(null, "Flights in the past can not be edited");
                } else {
                    Flight f = (Flight) ff.getSelectedFlight();
                    fd.setPlane(ebic.findPlaneByKey(f.getPlaneId()));
                    fd.setStartAirport(ebic.findAirportByKey(f.getStartAirportId()));
                    fd.setDestAirport(ebic.findAirportByKey(f.getDestAirportId()));
                    fd.setDate(f.getDateTime());
                    // pases the type which should be edited
                    fd.createAndShowDialog(f);
                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getActionCommand().equals("deleteFlight")) {
            try {
                if (ff.getSelectedFlight() == null) {
                    JOptionPane.showMessageDialog(null, "No flight selected");
                } else if (((Flight) ff.getSelectedFlight()).getDateTime().before(new Date())) {
                    JOptionPane.showMessageDialog(null, "Flights in the past can not be canceled");
                } else {
                    ff.setTableData(ebic.getAllFlights());
                    Flight f = (Flight) ff.getSelectedFlight();
                    int i = JOptionPane.showConfirmDialog(clientMainFrame, "Cancel Flight: " + f.getFlightId() + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if (i == JOptionPane.YES_OPTION) {
                        ebic.deleteFlight(f);
                        ff.setTableData(ebic.getAllFlights());
                        // cleans the view
                        ff.removeAll();
                        // renews the cf
                        ff.createAndShowForm();
                        // shows the cf again
                        ebg.setRightPanel(ff);
                    }

                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getActionCommand().equals("FlightDialogAirportSelect")) {
            JButton flightSource = (JButton) e.getSource();
            // Removes the Start Airport from the list of destinations
            fdas.setTableData(ebic.getAllAirports());
            fdas.setButtonName(flightSource.getName());
            fdas.createAndShowDialog();
        }
        if (e.getActionCommand().equals("FlightSelectAirport")) {
            JButton flightSource = (JButton) e.getSource();
            if (flightSource.getName().equals("startAirp")) {
                fd.setStartAirport((Airport) fdas.getSelectedAirport());
                fdas.dispose();
            }
            if (flightSource.getName().equals("destAirp")) {
                fd.setDestAirport((Airport) fdas.getSelectedAirport());
                fdas.dispose();
            }
        }
        if (e.getActionCommand().equals("FlightDialogDateSelect")) {
            try {
                if ((Flight) ff.getSelectedFlight() == null) {
                    fdds.setDate(new Date());
                } else {
                    Flight f = (Flight) ff.getSelectedFlight();
                    fdds.setDate(f.getDateTime());
                }
                fdds.createAndShowDialog();
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getActionCommand().equals("FlightDialogPlaneSelect")) {
            fdps.setTableData(ebic.getAllPlanes());
            fdps.createAndShowDialog();
        }
        if (e.getActionCommand().equals("FlightSelectPlane")) {
            fd.setPlane((Plane) fdps.getSelectedPlane());
            fdps.dispose();
        }
        if (e.getActionCommand().equals("FlightSelectDate")) {
            fd.setDate(fdds.getDate());
            fdds.dispose();
        }
        if (e.getActionCommand().equals("flightDialogOk")) {
            try {
                boolean checkTextFields = true;
                if (fd.getPlane() == null || fd.getStartAirport() == null || fd.getDestAirport() == null || fd.getDate() == null) {
                    JOptionPane.showMessageDialog(null, "Please enter all fields");
                    checkTextFields = false;
                }
                if (fd.getStartAirport().getAirportId() == fd.getDestAirport().getAirportId()) {
                    JOptionPane.showMessageDialog(null, "Start and destination airport are the same!");
                    checkTextFields = false;
                }
                if (fd.getDate().before(new Date())) {
                    JOptionPane.showMessageDialog(null, "Flight date is in the past!");
                    checkTextFields = false;
                }
                JButton flightSource = (JButton) e.getSource();
                if (flightSource.getName().equals("createFlight") && checkTextFields) {

                    ff.setTableData(ebic.getAllFlights());
                    // adds a new Type example data
                    ebic.createFlight(fd.getStartAirport().getAirportId(), fd.getDestAirport().getAirportId(), fd.getDate(), fd.getPlane().getPlaneId());
                    // gets and sets the whole table data
                    ff.setTableData(ebic.getAllFlights());
                    // cleans the view
                    ff.removeAll();
                    // renews the airpf
                    ff.createAndShowForm();
                    // shows the airpf again
                    ebg.setRightPanel(ff);
                    // closes the airpcd
                    fd.dispose();

                }
                if (flightSource.getName().equals("editFlight") && checkTextFields) {

                    ff.setTableData(ebic.getAllFlights());
                    Date d = fd.getDate();
                    // adds a new airport example data
                    ebic.updateFlight(fd.getStartAirport().getAirportId(), fd.getDestAirport().getAirportId(), fd.getDate(), fd.getPlane().getPlaneId(), fd.getEditableFlight());
                    // gets and sets the whole table data
                    ff.setTableData(ebic.getAllFlights());
                    // cleans the view
                    ff.removeAll();
                    // renews the airpf
                    ff.createAndShowForm();
                    // shows the ff again
                    ebg.setRightPanel(ff);
                    // closes the fed
                    fd.dispose();
                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getActionCommand().equals("createBooking")) {
            bd.createAndShowDialog();
        }
        if (e.getActionCommand().equals("editBooking")) {
            try {
                if (bf.getSelectedBooking() == null) {
                    JOptionPane.showMessageDialog(null, "No booking selected");
                } else if (ebic.findFlightByKey(((Booking) bf.getSelectedBooking()).getFlightId()).getDateTime().before(new Date())) {
                    JOptionPane.showMessageDialog(null, "Bookings in the past can not be edited");
                } else if (((Booking) bf.getSelectedBooking()).getIsCheckedIn()) {
                    JOptionPane.showMessageDialog(null, "Checked in bookings can not be edited");
                } else {
                    bd.createAndShowDialog((Booking) bf.getSelectedBooking());
                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getActionCommand().equals("deleteBooking")) {
            try {
                if (bf.getSelectedBooking() == null) {
                    JOptionPane.showMessageDialog(null, "No booking selected");
                } else if (ebic.findFlightByKey(((Booking) bf.getSelectedBooking()).getFlightId()).getDateTime().before(new Date())) {
                    JOptionPane.showMessageDialog(null, "Bookings in the past can not be canceled");
                } else if (((Booking) bf.getSelectedBooking()).getIsCheckedIn()) {
                    JOptionPane.showMessageDialog(null, "Checked in bookings can not be canceled");
                } else {
                    bf.setTableData(ebic.getAllBookings());
                    Booking b = (Booking) bf.getSelectedBooking();
                    int i = JOptionPane.showConfirmDialog(clientMainFrame, "Cancel Booking No: " + b.getBookingId() + " ?", "Confirmation", JOptionPane.YES_NO_OPTION);
                    if (i == JOptionPane.YES_OPTION) {
                        ebic.deleteBooking(b);
                        bf.setTableData(ebic.getAllBookings());
                        // cleans the view
                        bf.removeAll();
                        // renews the cf
                        bf.createAndShowForm();
                        // shows the cf again
                        ebg.setRightPanel(bf);
                    }
                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (e.getActionCommand().equals("BookingDialogFlightSelect")) {
            bdfs.setTableData(ebic.getAllFlights());
            bdfs.createAndShowDialog();
        }

        if (e.getActionCommand().equals("BookingDialogCustomerSelect")) {
            bdcs.setTableData(ebic.getAllCustomers());
            bdcs.createAndShowDialog();
        }

        if (e.getActionCommand().equals("BookingDialogSeatSelect")) {
            bdss = new BookingDialogSeatSelect(this);
            if (bd.getFlight() != null) {
                bdss.createAndShowDialog(bd.getFlight());
            } else {
                JOptionPane.showMessageDialog(null, "Please select Flight first");
            }
        }
        if (e.getActionCommand().equals("BookingSelectFlight")) {
            bd.setFlight((Flight) bdfs.getSelectedFlight());
            bdfs.dispose();

        }
        if (e.getActionCommand().equals("BookingSelectCustomer")) {
            bd.setCustomer((Customer) bdcs.getSelectedCustomer());
            bdcs.dispose();
        }

        if (e.getActionCommand().equals("BookingSelectSeat")) {
            bd.setSeat((Seat) bdss.getSelectedSeat());
            bdss.dispose();
        }

        if (e.getActionCommand().equals("bookingDialogOk")) {
            boolean checkTextFields = true;
            if (bd.getBookingDate() == null || bd.getFlight() == null || bd.getSeat() == null || bd.getCustomer() == null) {
                JOptionPane.showMessageDialog(null, "Please enter all fields");
                checkTextFields = false;
            }
            JButton bookingSource = (JButton) e.getSource();
            if (bookingSource.getName().equals("createBooking") && checkTextFields) {
                try {
                    bf.setTableData(ebic.getAllBookings());
                    // adds a new Type example data
                    ebic.createBooking(bd.getBookingDate(), bd.getCustomer().getCustomerId(), bd.getFlight().getFlightId(), bd.getSeat().getSeatId());
                    // gets and sets the whole table data
                    bf.setTableData(ebic.getAllBookings());
                    // cleans the view
                    bf.removeAll();
                    // renews the airpf
                    bf.createAndShowForm();
                    // shows the airpf again
                    ebg.setRightPanel(bf);
                    // closes the airpcd
                    bd.dispose();
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (bookingSource.getName().equals("editBooking") && checkTextFields) {
                try {
                    bf.setTableData(ebic.getAllBookings());
                    // adds a new Type example data
                    ebic.updateBooking(bd.getBookingDate(), bd.getCustomer().getCustomerId(), bd.getFlight().getFlightId(), bd.getSeat().getSeatId(), bd.getEditableBooking());
                    // gets and sets the whole table data
                    bf.setTableData(ebic.getAllBookings());
                    // cleans the view
                    bf.removeAll();
                    // renews the airpf
                    bf.createAndShowForm();
                    // shows the airpf again
                    ebg.setRightPanel(bf);
                    // closes the airpcd
                    bd.dispose();
                } catch (RemoteException ex) {
                    Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        if (e.getActionCommand().equals("checkInBooking")) {
            try {
                if (bf.getSelectedBooking() == null) {
                    JOptionPane.showMessageDialog(null, "No booking selected");
                } else if (ebic.findFlightByKey(((Booking) bf.getSelectedBooking()).getFlightId()).getDateTime().before(new Date())) {
                    JOptionPane.showMessageDialog(null, "Bookings of flights in the past can not be checked in");
                } else {
                    bf.setTableData(ebic.getAllBookings());
                    ebic.checkInBooking((Booking) bf.getSelectedBooking());
                    bf.setTableData(ebic.getAllBookings());
                    bf.removeAll();
                    bf.createAndShowForm();
                    ebg.setRightPanel(bf);
                    bd.dispose();
                }
            } catch (RemoteException ex) {
                Logger.getLogger(EBInterface.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getActionCommand().equals("refresh")) {
            JButton sourceButton = (JButton) e.getSource();
            if (sourceButton.getName().equals("refreshBookings")) {

                bf.setTableData(ebic.getAllBookings());
                bf.removeAll();
                bf.createAndShowForm();
                ebg.setRightPanel(bf);
            }
             if (sourceButton.getName().equals("refreshFlights")) {
                ff.setTableData(ebic.getAllFlights());
                ff.removeAll();
                ff.createAndShowForm();
                ebg.setRightPanel(ff);
            }
             if (sourceButton.getName().equals("refreshCustomers")) {
                cf.setTableData(ebic.getAllCustomers());
                cf.removeAll();
                cf.createAndShowForm();
                ebg.setRightPanel(cf);
            }
             if (sourceButton.getName().equals("refreshAirlines")) {
                airlf.setTableData(ebic.getAllAirlines());
                airlf.removeAll();
                airlf.createAndShowForm();
                ebg.setRightPanel(airlf);
            }
            if (sourceButton.getName().equals("refreshAirports")) {
                airpf.setTableData(ebic.getAllAirports());
                airpf.removeAll();
                airpf.createAndShowForm();
                ebg.setRightPanel(airpf);
            }
            if (sourceButton.getName().equals("refreshTypes")) {
                tf.setTableData(ebic.getAllTypes());
                tf.removeAll();
                tf.createAndShowForm();
                ebg.setRightPanel(tf);
            }
             if (sourceButton.getName().equals("refreshPlanes")) {
                pf.setTableData(ebic.getAllPlanes());
                pf.removeAll();
                pf.createAndShowForm();
                ebg.setRightPanel(pf);
            }


        }
    }

    public void valueChanged(TreeSelectionEvent e) {
        TreePath path = e.getNewLeadSelectionPath();
        Object a = path.getLastPathComponent().toString();
        if (a == "Customers") {
            // removes all old components
            cf.removeAll();
            // set table date
            cf.setTableData(ebic.getAllCustomers());
            // prepare form for displaying
            cf.createAndShowForm();
            // show cf in the right panel
            ebg.setRightPanel(cf);
        }

        if (a == "Types") {
            tf.removeAll();
            tf.setTableData(ebic.getAllTypes());
            tf.createAndShowForm();
            ebg.setRightPanel(tf);
        }

        if (a == "Airports") {
            airpf.removeAll();
            airpf.setTableData(ebic.getAllAirports());
            airpf.createAndShowForm();
            ebg.setRightPanel(airpf);
        }

        if (a == "Planes") {
            pf.removeAll();
            pf.setTableData(ebic.getAllPlanes());
            pf.createAndShowForm();
            ebg.setRightPanel(pf);
        }

        if (a == "Airlines") {
            airlf.removeAll();
            airlf.setTableData(ebic.getAllAirlines());
            airlf.createAndShowForm();
            ebg.setRightPanel(airlf);
        }

        if (a == "Flights") {
            ff.removeAll();
            ff.setTableData(ebic.getAllFlights());
            ff.createAndShowForm();
            ebg.setRightPanel(ff);
        }

        if (a == "Bookings") {
            bf.removeAll();
            bf.setTableData(ebic.getAllBookings());
            bf.createAndShowForm();
            ebg.setRightPanel(bf);
        }

        if (a == "EasyBooking") {
            ebg.setRightPanel(ebg.getMainPanel());
        }
    }
}
